package chat.process;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import chat.dao.MessageDAO;
import chat.model.Message;

public class ChatServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession();
		if (session.getAttribute("currentUser") == null) {
			response.sendRedirect("/login");
		}
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE html><html><head><title>Html chat</title><link rel='stylesheet' type='text/css' href='css/style.css'><script type='text/javascript' src='js/chat.js'></script><script type='text/javascript' src='js/util.js'></script><script text='text/javascript'></script></head><body><div id='wrapper'><!-- khung hiển thị nội dung chat --><iframe id='output'> </iframe><!-- tuy chinh noi dung hien thi --><table id='toolbar'><tr><td><select id='fontname'><option value='Font'>Font</option><option value='Arial'>Arial</option><option value='Courier'>Courier</option><option value='Times New Roman'>Times New Roman</option></select></td><td><select id='fontsize' unselectable='on' selected='0'><option value='size'>Size</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option></select></td><td><div id='bold' class='imagebutton'><img class='image' title='Bold' alt='Bold' src='images/bold.gif' /></div></td><td><div id='italic' class='imagebutton'><img class='image' title='Italic' alt='Italic'src='images/italic.gif' /></div></td><td><div id='underline' class='imagebutton'><img class='image' title='Underline' alt='Underline'src='images/underline.gif' /></div></td><td><div id='forecolor' class='imagebutton'><img class='image' title='Text Color' alt='Text Color'src='images/forecolor.gif' /></div></td><td><span id='button-choose-icon' rel='toggle'class='emoteTogglerImg'></span></td><tr></table><table cellspacing='0' cellpadding='4' border='0' id='select-icon'><tr><td><img border='0' class='inlineimg' title='online'alt='(:-O)' id='vB_Editor_001_smilie_46' src='images/online.gif'style='cursor: pointer;'></td><td><img border='0' class='inlineimg' title='online'alt='(:-O)' id='vB_Editor_001_smilie_46' src='images/thinking.gif'style='cursor: pointer;'></td><td><img border='0' class='inlineimg' title='online'alt='(:-O)' id='vB_Editor_001_smilie_46' src='images/wave.gif'style='cursor: pointer;'></td><td><img border='0' class='inlineimg' title='online'alt='(:-O)' id='vB_Editor_001_smilie_46' src='images/laughing.gif'style='cursor: pointer;'></td></tr></table><!-- khung nhập nội dung chat --><iframe id='edit'></iframe><div id='colorpalette' width='250' height='170'style='visibility: hidden; position: absolute; z-index: 99999;'src='colors.html'><table cellspacing='1' cellpadding='1' border='1'><tbody><tr><td width='20' height='20' bgcolor='#FFFFFF' id='#FFFFFF'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FFCCCC' id='#FFCCCC'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FFCC99' id='#FFCC99'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFFF99' id='#FFFF99'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFFFCC' id='#FFFFCC'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#99FF99' id='#99FF99'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#99FFFF' id='#99FFFF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#CCFFFF' id='#CCFFFF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#CCCCFF' id='#CCCCFF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFCCFF' id='#FFCCFF'><imgwidth='1' height='1'></td></tr><tr><td width='20' height='20' bgcolor='#CCCCCC' id='#CCCCCC'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FF6666' id='#FF6666'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FF9966' id='#FF9966'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFFF66' id='#FFFF66'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFFF33' id='#FFFF33'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#66FF99' id='#66FF99'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#33FFFF' id='#33FFFF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#66FFFF' id='#66FFFF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#9999FF' id='#9999FF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FF99FF' id='#FF99FF'><imgwidth='1' height='1'></td></tr><tr><td width='20' height='20' bgcolor='#C0C0C0' id='#C0C0C0'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FF0000' id='#FF0000'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FF9900' id='#FF9900'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FFCC66' id='#FFCC66'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#FFFF00' id='#FFFF00'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#33FF33' id='#33FF33'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#66CCCC' id='#66CCCC'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#33CCFF' id='#33CCFF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#6666CC' id='#6666CC'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#CC66CC' id='#CC66CC'><imgwidth='1' height='1'></td></tr><tr><td width='20' height='20' bgcolor='#999999' id='#999999'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#CC0000' id='#CC0000'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FF6600' id='#FF6600'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFCC33' id='#FFCC33'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#FFCC00' id='#FFCC00'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#33CC00' id='#33CC00'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#00CCCC' id='#00CCCC'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#3366FF' id='#3366FF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#6633FF' id='#6633FF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#CC33CC' id='#CC33CC'><imgwidth='1' height='1'></td></tr><tr><td width='20' height='20' bgcolor='#666666' id='#666666'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#990000' id='#990000'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#CC6600' id='#CC6600'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#CC9933' id='#CC9933'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#999900' id='#999900'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#009900' id='#009900'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#339999' id='#339999'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#3333FF' id='#3333FF'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#6600CC' id='#6600CC'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#993399' id='#993399'><imgwidth='1' height='1'></td></tr><tr><td width='20' height='20' bgcolor='#333333' id='#333333'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#660000' id='#660000'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#993300' id='#993300'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#996633' id='#996633'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#666600' id='#666600'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#006600' id='#006600'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#336666' id='#336666'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#000099' id='#000099'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#333399' id='#333399'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#663366' id='#663366'><imgwidth='1' height='1'></td></tr><tr><td width='20' height='20' bgcolor='#000000' id='#000000'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#330000' id='#330000'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#663300' id='#663300'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#663333' id='#663333'style='border: 1px solid gray;'><img width='1' height='1'></td><td width='20' height='20' bgcolor='#333300' id='#333300'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#003300' id='#003300'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#003333' id='#003333'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#000066' id='#000066'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#330099' id='#330099'><imgwidth='1' height='1'></td><td width='20' height='20' bgcolor='#330033' id='#330033'><imgwidth='1' height='1'></td></tr></tbody></table></div><input type='button' id='send-button' class='button-disable'value='Send' /></div></body></html>");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String action = request.getParameter("action");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String user = session.getAttribute("currentUser").toString();

		if (action.equals("getuser")) {
			out.print(user);
		} else if (action.equals("save")) {
			String message = request.getParameter("message");
			// out.print(message);
			Message msg = new Message();
			Timestamp date = new Timestamp(new java.util.Date().getTime());
			msg.setPostedDate(date);
			msg.setUsername(user);
			msg.setMessage(message);
			int t = MessageDAO.saveMessage(msg);
			out.print(t);
		} else if (action.equals("getbefore")) {
			String json = new Gson().toJson(MessageDAO.getAllMessage());
			response.getWriter().write(json);
		} else if (action.equals("getupdate")) {
			String time = request.getParameter("time");
			System.out.println("time: " + time);
			Timestamp lastupdate = new Timestamp(Long.parseLong(time));
			if (MessageDAO.getNewMessage(lastupdate) == null) {
				out.print("nothing");
			} else {
				String json = new Gson().toJson(MessageDAO
						.getNewMessage(lastupdate));
				response.getWriter().write(json);
			}
		}
	}
}
